var app = require('express')();
var http = require('http').Server(app);
var session = require('express-session');
var parseData = require('./data/ParseProtoData');

app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true,
    cookie: { secure: false }
}));

//设置跨域访问
app.all("*", function (req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "content-type");
    //跨域允许的请求方式 
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200);  //让options尝试请求快速结束
    else
        next();
});

app.get('/getServerInfo', function (req, res) {
    // res.send('<h1>Hello world</h1>');
    let data = {
        username: "yeppt",
        password: "123456",
        loginType: 2,
        sceneType: 2
    }
    let e1 = parseData.scData('com.bochsler.protocol.LoginRequest', 10001, data);
    let e = parseData.toBuffer(e1)
    console.log('----', e.length, e);
    var params = { code: 0, data: e }
    res.send(params);
});

app.get('/', function (req, res) {
    res.send('<h1>Hello world</h1>');
    let data = {
        username: "yeppt",
        password: "123456",
        loginType: 2,
        sceneType: 2
    }
    let e = parseData.encode('com.bochsler.protocol.LoginRequest', data);
    console.log('----', e.length, e);

    let d = parseData.decode('com.bochsler.protocol.LoginRequest', e);
    console.log('----', d.length, d);

    let e1 = parseData.scData('com.bochsler.protocol.LoginRequest', 10001, data);
    console.log('----', e1.length, e1);

});

http.listen(3000, function () {
    console.log('Server running at http://127.0.0.1:3000/');
});